package org.dong.Control;


import com.alibaba.fastjson.JSON;
import org.dong.Annotation.MyControl;
import org.dong.Annotation.MyRequestMapping;
import org.dong.Tool.ApplicationUtil;
import org.dong.Tool.Data;
import org.dong.pojo.AdminUser;
import org.dong.pojo.Dto;
import org.dong.service.Impl.ReceptionServiceImpl;
import org.dong.service.ReceptionService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax .servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class AdminUserControl {

    @Resource
    ReceptionService receptionService;

    @PostMapping("/adminUser/login")
    @CrossOrigin(origins = "*")
    public Dto AdminLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Dto dto = new Dto();

        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String code = req.getParameter("code");


        if (("1234").equalsIgnoreCase((String) req.getSession().getAttribute("codeStr"))) {

            AdminUser adminUser = receptionService.checkLogin(account, password);
            if (adminUser != null) {
                req.getSession().setAttribute("user",adminUser);
                Map<String, Object> datas = new HashMap<String, Object>();
                datas.put("user", adminUser);
                dto.setDatas(datas);
                dto.setErrMsg("登录成功");
                dto.setLocation("main");
                dto.setStatusCode(200);

            } else {
                dto.setErrMsg("登录失败");
                dto.setLocation("");
                dto.setStatusCode(2002);
            }
        } else {
            dto.setErrMsg("验证码错误");
            dto.setLocation("");
            dto.setStatusCode(2001);
        }

        return dto;
    }
    @RequestMapping("/adminUser/logout")
    public String AdminLogout(HttpSession session){
        session.invalidate();

        return "/";
    }
}

